package board;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class FreeCommentAction extends DispatchAction {

	/**
	 * 코멘트 작성 메소드
	 * */
	public ActionForward write(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		int fbno = Integer.parseInt(request.getParameter("fbno"));
		String mid = request.getParameter("mid");
		int cono = Integer.parseInt(request.getParameter("cono"));
		String fccomment = URLDecoder.decode(request.getParameter("fccomment"),"utf-8");
		
		System.out.println("comment : "+fccomment);
		
		FreeCommentDAO fcDAO = new FreeCommentDAO();
		FreeComment fc = new FreeComment();
		fc.setFbno(fbno);
		fc.setMid(mid);
		fc.setFccomment(fccomment);
		fc.setIconno(1);
		
		fc = fcDAO.insert(fc);
		
		StringBuffer sb = new  StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		
		String mcnick = MemberCommunityDAO.getCommunityNick(mid, cono);
		
			sb.append("<comment>");
			sb.append("<fcno>"+fc.getFcno()+"</fcno>");
			sb.append("<fbno>"+fc.getFbno()+"</fbno>");
			sb.append("<mid>"+fc.getMid()+"</mid>");
			sb.append("<mcnick>"+mcnick+"</mcnick>");
			sb.append("<fccomment>"+fc.getFccomment()+"</fccomment>");
			sb.append("<fcdate>"+fc.getFcdate().substring(0,19)+"</fcdate>");
			sb.append("<iconno>"+fc.getIconno()+"</iconno>");
			sb.append("</comment>");
		
		sb.append("</comments>");
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);
		out.close();
		return null;
	}
	
	/**
	 * 코멘트 삭제 메소드
	 * */
	public ActionForward remove(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		int fcno = Integer.parseInt(request.getParameter("fcno"));
		
		FreeCommentDAO fcDAO = new FreeCommentDAO();
		
		StringBuffer sb = new  StringBuffer();
		if(fcDAO.delete(fcno)) sb.append("removed");			
		else sb.append("failed");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);	
		out.close();	
		
		return null;
	}
	
	/**
	 * 코멘트 리스트 호출 메소드
	 * */
	public ActionForward getCommmentList(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		request.setCharacterEncoding("utf-8");
		int fbno = Integer.parseInt(request.getParameter("fbno"));
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		FreeCommentDAO fcDAO = new FreeCommentDAO();
		Vector<FreeComment> list = fcDAO.getFreeCommentList(fbno);
		
		StringBuffer sb = new  StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		for(int i=0;i<list.size();i++){
			String mcnick = MemberCommunityDAO.getCommunityNick(list.get(i).getMid(), cono);
			
			sb.append("<comment>");
			sb.append("<fcno>"+list.get(i).getFcno()+"</fcno>");
			sb.append("<fbno>"+list.get(i).getFbno()+"</fbno>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");
			sb.append("<mcnick>"+mcnick+"</mcnick>");
			sb.append("<fccomment>"+list.get(i).getFccomment()+"</fccomment>");
			sb.append("<fcdate>"+list.get(i).getFcdate().substring(0,19)+"</fcdate>");
			sb.append("<iconno>"+list.get(i).getIconno()+"</iconno>");
			sb.append("</comment>");
		}
		sb.append("</comments>");
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);		
		out.close();
		
		return null;
	}
	
	public static void main(String[] args){
		
		FreeCommentDAO fcDAO = new FreeCommentDAO();
		Vector<FreeComment> list = fcDAO.getFreeCommentList(8);
		
		StringBuffer sb = new  StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		for(int i=0;i<list.size();i++){
			sb.append("<comment>");
			sb.append("<fcno>"+list.get(i).getFcno()+"</fcno>");
			sb.append("<fbno>"+list.get(i).getFbno()+"</fbno>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");
			sb.append("<fccomment>"+list.get(i).getFccomment()+"</fccomment>");
			sb.append("<fcdate>"+list.get(i).getFcdate().substring(0,18)+"</fcdate>");
			sb.append("<iconno>"+list.get(i).getIconno()+"</iconno>");
			sb.append("</comment>");
		}
		sb.append("</comments>");
		
		System.out.println(sb);
		
	}
	
	/**
	 * 코멘트 호출 메소드
	 * */
	public ActionForward getCommment(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		return null;
	}
	
	

}
